<?php

/**
 * Base class for all application presenters.
 *
 * @author     John Doe
 * @package    MyApplication
 */
use Nette\Application\UI\Form,
	Nette\Security as NS;

abstract class BasePresenter extends Nette\Application\UI\Presenter {

    /**
     * @var Nette\Database\Connection 
     */
    protected $db;

    protected function startup() {
	parent::startup();
	$this->template->form = 'sign';
	$this->db = $this->context->database;
    }

    /**
     * Sign in form component factory.
     * @return Nette\Application\UI\Form
     */
    protected function createComponentSignInForm() {
	$form = new Form;
	$form->addText('login')
		->setDefaultValue('Vaše přihlašovací jméno.')
		->setRequired('Prosím vyplňte váše přihlašovací jméno.');

	$form->addPassword('password')
		->setDefaultValue('Heslo')
		->setRequired('Zadejte heslo.');

	$form->addSubmit('send', 'PŘIHLÁSIT SE');

	$form->onSuccess[] = callback($this, 'signInFormSubmitted');
	return $form;
    }

    public function signInFormSubmitted($form) {
	try {
	    $values = $form->getValues();
	    $this->getUser()->login($values->login, $values->password);
	    $this->redirect('Homepage:default');
	} catch (NS\AuthenticationException $e) {
	    //$form->addError($e->getMessage());
	    $this->flashMessage($e->getMessage());
	}
    }

}
